package com.itheima.config;


import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;


import javax.sql.DataSource;

public class MyBatisConfig {
    //创建方法获取SqlSessionFactoryBean对象,获取到的对象添加到Ioc容器中
    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(@Qualifier("druidDataSource") DataSource dataSource) {
        //创建mybatis和spring关联的工厂对象
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        //给工厂对象属性赋值     数据库资源对象  DataSource 为通过数据库配置类中获取
        factoryBean.setDataSource(dataSource);
        //配置包中类的别名
        factoryBean.setTypeAliasesPackage("com.itheima.domain");

        return factoryBean;
    }
    //创建方法获取MapperScannerConfigurer对象,将获取到的对象添加到ioc容器中
    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer(){
        //创建mapper包中接口扫描对象
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        //设置持久层包接口的基础包的路径
        mapperScannerConfigurer.setBasePackage("com.itheima.mapper");
        return mapperScannerConfigurer;
    }
}
